package homework;

import java.io.*;
import java.util.HashMap;

/**
 * @classname:demo07
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/16 19:20
 * @version: 0.1
 * @since:1.8
 */
public class demo07 {
    public static void main(String[] args) {
        String pathName = "day16/src/a.txt";
        checkNum(pathName);

    }

    private static void checkNum(String pathName) {

        try (InputStream in = new FileInputStream(pathName);
             OutputStream out = new FileOutputStream("day16/src/b.txt");

        ) {
            byte[] bytes = new byte[1024];
            int len ;
            String str ;
            while ((len = in.read(bytes)) != -1) {
                str = new String(bytes, 0, len);
                HashMap<String, Integer> map = charNum(str);
                String string = map.toString();
                byte[] bytes1 = string.getBytes();
                out.write(bytes1);
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static HashMap<String, Integer> charNum(String str) {

        HashMap<String, Integer> map = new HashMap<>();
        for (int i = 0; i < str.length(); i++) {
            char charAt = str.charAt(i);
            String charStr = String.valueOf(charAt);
            Integer count = map.get(charStr);
            if (count == null) {
                map.put(charStr, 1);
            } else {
                count++;
                map.put(charStr, count);
            }
        }
        return map;

    }


}
